package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @RequestMapping("/tofile")
    public String tofileupload(){
        return "fileupload";
    }
    @ResponseBody
    @RequestMapping("/test1")
    public String test1(MultipartFile source, @RequestBody String name, HttpServletRequest request) throws IOException {
        String filename = source.getOriginalFilename();
        System.out.println(name);
        String realPath = request.getServletContext().getRealPath("/upload");
        System.out.println("=====realPath==="+realPath);
//        source.transferTo(new File(realPath+"\\"+filename));
        source.transferTo(new File(realPath + "\\" + filename));
        return "http://localhost:8080/upload/"+filename;
    }
}
